package com.js.statemachine.config.uml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.persist.RepositoryStateMachinePersist;
import org.springframework.statemachine.redis.RedisStateMachineContextRepository;
import org.springframework.statemachine.redis.RedisStateMachinePersister;

/**
 * @author ：js
 * @date ：Created in 2021-03-02 9:38
 * @description: 持久化配置
 * @version: 1.0
 */
@Configuration
public class MeterPersistUmlConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;


    /**
     * 注入RedisStateMachinePersister对象
     *
     * @return
     */
    @Bean(name = "meterRedisUmlPersister")
    public RedisStateMachinePersister<String, String> meterRedisPersister() {
        return new RedisStateMachinePersister<>(meterRedisPersist());
    }

    /**
     * 通过redisConnectionFactory创建StateMachinePersist
     *
     * @return
     */
    public StateMachinePersist<String, String, String> meterRedisPersist() {
        RedisStateMachineContextRepository<String, String> repository = new RedisStateMachineContextRepository<>(redisConnectionFactory);
        return new RepositoryStateMachinePersist<>(repository);
    }




}
